﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace KINECTKey
{
    using System.Windows.Media.Animation;

    /// <summary>
    /// KINECTKey.xaml の相互作用ロジック
    /// </summary>
    public partial class KINECTKey : UserControl
    {
        public KINECTKey()
        {
            InitializeComponent();
        }

        public enum KeyKind
        {
            CenterKey = 0x0001,
            TopKey = 0x0002,
            LeftKey = 0x0004,
            BottomKey = 0x0008,
            RightKey = 0x0010
        }

        public KeyKind GetKeyKind(int x, int y)
        {
            return KeyKind.CenterKey;
        }

        public string CenterKeyString { get; set; }
        public string TopKeyString { get; set; }
        public string LeftKeyString { get; set; }
        public string BottomKeyString { get; set; }
        public string RightKeyString { get; set; }

        private Dictionary<KeyKind, string> keyAssign = new Dictionary<KeyKind, string>();
        private Dictionary<KeyKind, Shape> keyShape = new Dictionary<KeyKind, Shape>();

        public void SelectCenterKey()
        {
            MorphingSelectionKey(true, CenterKey);
            MorphingTopKey(true);
            MorphingLeftKey(true);
            MorphingBottomKey(true);
            MorphingRightKey(true);
        }

        public void DeselectCenterKey()
        {
            MorphingSelectionKey(false, CenterKey);
            MorphingTopKey(false);
            MorphingLeftKey(false);
            MorphingBottomKey(false);
            MorphingRightKey(false);
        }

        public void SelectAroundKey(KeyKind keyKind)
        {
            switch (keyKind)
            {
                case KeyKind.TopKey:
                    MorphingSelectionKey(true, CenterKey);
                    break;
                case KeyKind.LeftKey:
                    MorphingSelectionKey(true, LeftKey);
                    break;
                case KeyKind.BottomKey:
                    MorphingSelectionKey(true, BottomKey);
                    break;
                case KeyKind.RightKey:
                    MorphingSelectionKey(true, RightKey);
                    break;
            }
        }
        private void MorphingSelectionKey(bool select, Shape target)
        {
            double fromScaleX = 1.1;
            double fromScaleY = 1.1;
            double toScaleX = 1.0;
            double toScaleY = 1.0;
            if (select)
            {
                fromScaleX = 1.0;
                fromScaleY = 1.0;
                toScaleX = 1.1;
                toScaleY = 1.1;
            }
            ScaleTransform ckTx = new ScaleTransform();
            ckTx.CenterX = target.ActualWidth / 2;
            ckTx.CenterY = target.ActualHeight / 2;
            ckTx.ScaleX = fromScaleX;
            ckTx.ScaleY = fromScaleY;
            CenterKey.RenderTransform = ckTx;
            ckTx.BeginAnimation(ScaleTransform.ScaleXProperty,
                new DoubleAnimation(toScaleX, new Duration(TimeSpan.FromMilliseconds(1000))));
            ckTx.BeginAnimation(ScaleTransform.ScaleYProperty,
                new DoubleAnimation(toScaleY, new Duration(TimeSpan.FromMilliseconds(1000))));
        }

        private void MorphingTopKey(bool show)
        {
            double toY = 60;
            TranslateTransform ttx = new TranslateTransform();
            ttx.Y = 0;
            ttx.X = 0;
            if (show)
            {
                toY = -60;
            }
            TopKey.RenderTransform = ttx;
            ttx.BeginAnimation(TranslateTransform.YProperty,
                new DoubleAnimation(toY, new Duration(TimeSpan.FromMilliseconds(1000))));
        }

        private void MorphingBottomKey(bool show)
        {
            double toY = -60;
            TranslateTransform ttx = new TranslateTransform();
            ttx.Y = 0;
            ttx.X = 0;
            if (show)
            {
                toY = 60;
            }
            BottomKey.RenderTransform = ttx;
            ttx.BeginAnimation(TranslateTransform.YProperty,
                new DoubleAnimation(toY, new Duration(TimeSpan.FromMilliseconds(1000))));
        }

        private void MorphingLeftKey(bool show)
        {
            double toX = 60;
            TranslateTransform ttx = new TranslateTransform();
            ttx.Y = 0;
            ttx.X = 0;
            if (show)
            {
                toX = -60;
            }
            LeftKey.RenderTransform = ttx;
            ttx.BeginAnimation(TranslateTransform.XProperty,
                new DoubleAnimation(toX, new Duration(TimeSpan.FromMilliseconds(1000))));
        }

        private void MorphingRightKey(bool show)
        {
            double toX = -60;
            TranslateTransform ttx = new TranslateTransform();
            ttx.Y = 0;
            ttx.X = 0;
            if (show)
            {
                toX = 60;
            }
            LeftKey.RenderTransform = ttx;
            ttx.BeginAnimation(TranslateTransform.XProperty,
                new DoubleAnimation(toX, new Duration(TimeSpan.FromMilliseconds(1000))));
        }


        public void FocusKey(int x, int y)
        {
            foreach (var key in keyAssign.Keys)
            {
                if (!String.IsNullOrEmpty(keyAssign[key]))
                {
                    
                    keyShape[key].Visibility = System.Windows.Visibility.Visible;
                }
            }

        }

        public void UnfocusKeys()
        {
            foreach (var key in keyAssign.Keys)
            {
                keyShape[key].Opacity = 30;
            }
        }

        public void HideAroundKey()
        {
            foreach (var key in keyAssign.Keys)
            {
                if (key!=KeyKind.CenterKey)
                {
                    keyShape[key].Visibility = System.Windows.Visibility.Hidden;
                }
            }

            
        }
    }
}
